package com.ys.zero.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.google.gson.Gson;
import com.ys.zero.bean.Dict;
import com.ys.zero.bean.Uarticle;
import com.ys.zero.bean.User;
import com.ys.zero.bean.Video;
import com.ys.zero.dao.VideoDao;
import com.ys.zero.dao.articalDao;
/**
 * 
 * 渲染界面Servlet
 *
 */
@WebServlet("/show/*")
public class showServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private articalDao adao = new articalDao();
	private VideoDao vdao = new VideoDao();
	
	/**
	 * 显示文章
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void artical(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		String uaid = request.getParameter("uaid");
		List<Uarticle> list = adao.showArtical(uaid);
		toJson(response, list);
	}
	
	/**
	 * 头条
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void handNews(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Uarticle> list = adao.showHeadNews();
		toJson(response, list);
	}
	
	
	/**
	 * 热门文章
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void articalHot(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Uarticle> list = adao.showHot();
		toJson(response, list);
	}
	/**
	 * 随便看看
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void random(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Uarticle> list = adao.showRandom();
		toJson(response, list);
	}
	
	
	/**
	 * 热门标签
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void log(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Uarticle> list = adao.showLog();
		toJson(response, list);
	}
	
	
	/**
	 * 热门阅读
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void rank(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Uarticle> list = adao.showRank();
		toJson(response, list);
	}
	/**
	 * 创作排行
	 * @throws Exception 
	 */
	protected void create(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Map<String, Object>> list = adao.showCreate();
		toJson(response, list);
	}
	
	/**
	 * 显示标签的前四个文章
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void content(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String[] logs = request.getParameterValues("log");
		List<List<Uarticle>> contents = new ArrayList<>(); 
		for(String log:logs) {
			contents.add(adao.showContent(log));
		}

		toJson(response, contents);
	}
	
	/**
	 * 大家热门搜索的logo
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void seacherLog(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Dict> list = adao.showSeacherLog();
		toJson(response, list);
	}
	
	
	//显示视频播放界面
	
	protected void showVideo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String vid = request.getParameter("vid");
		int vid2 = Integer.parseInt(vid);
		System.out.println("vid:"+vid);
		List<Video> list = vdao.showVideo(vid2);
		toJson(response, list);
	}
	/**
	 * 猜你喜欢
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void showLike(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Uarticle> list = adao.showRandom();
		toJson(response, list);
	}
	/**
	 * 显示文章收藏
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void showWZSC(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		User user = (User) request.getSession().getAttribute("loginedUser");
		List<Map<String, Object>>  list = adao.showWZSC(user.getAccount());
		toJson(response, list);
	}
}
